package me.andy.superappsorganizer.model;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.util.Log;

public class MarketData {

	public static String getMarketData(String app) {
		HttpResponse response = getMarketHTTP(app);
		StringBuilder string = null;
		int startSearch=0;
		int endSearch=0;
		
		try {
			string = inputStreamToString(response.getEntity().getContent());
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		startSearch = string.indexOf("<dt>Category:</dt>");
		endSearch = string.indexOf("<dt>Installs:</dt>",startSearch);
		return string.substring(startSearch, endSearch);
		
	}
	
	private static HttpResponse getMarketHTTP(String app) {
		// Create a new HttpClient and Post Header
		HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
		HttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost("http://market.android.com/details");

		try {
		    // Add your data
		    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		    nameValuePairs.add(new BasicNameValuePair("id", app));
		    nameValuePairs.add(new BasicNameValuePair("realname", "George+the+Wonder+Boy"));
		    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

		    // Execute HTTP Post Request
		    HttpResponse response = httpclient.execute(httppost);
		    return response;
		} catch (ClientProtocolException e) {
		    Log.e("ANDY", e.getMessage());
		} catch (IOException e) {
			Log.e("ANDY", e.getMessage());
		}
		return null;
	}
	
	// Fast Implementation
	private static StringBuilder inputStreamToString(InputStream is) {
	    String line = "";
	    StringBuilder total = new StringBuilder();
	    
	    // Wrap a BufferedReader around the InputStream
	    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

	    // Read response until the end
	    try {
			while ((line = rd.readLine()) != null) { 
			    total.append(line); 
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    // Return full string
	    return total;
	}


	
}


